package cn.edu.zjx2.service.impl;

import cn.edu.mp.entity.Class;
import cn.edu.mp.entity.Grade;
import cn.edu.mp.mapper.ClassMapper;
import cn.edu.mp.service.ClassService;
import cn.edu.mp.service.GradeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ClassServiceImpl extends ServiceImpl<ClassMapper, Class> implements ClassService {

    @Autowired
    private GradeService gradeService;

    @Override
    public List<Class> list() {
        // 获取所有班级
        List<Class> classes = super.list();
        if (classes.isEmpty()) {
            return classes;
        }

        // 获取所有年级
        List<Grade> grades = gradeService.list();
        Map<Long, String> gradeMap = grades.stream()
                .collect(Collectors.toMap(Grade::getId, Grade::getGradeName));

        // 设置年级名称
        classes.forEach(clazz -> 
            clazz.setGradeName(gradeMap.get(clazz.getGradeId()))
        );

        return classes;
    }
} 